//
//  LocalAlbumCell.m
//  OGL
//
//  Created by 洋 耿 on 15/4/8.
//  Copyright (c) 2015年 ZengYuan. All rights reserved.
//

#import "LocalAlbumCell.h"
#import "Config.h"

#import "PictureManager.h"

#define OFFSET 10
@interface LocalAlbumCell()
@property (nonatomic,weak) UIImageView *iconView;
@property (nonatomic,weak) UILabel *nameLabel;
@property (nonatomic,weak) UILabel *detailLabel;

@end

@implementation LocalAlbumCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"localAlbum";
    LocalAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[LocalAlbumCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self addSubviews];
        
        [self addConstraints];
        
        HorizonLine *devider = [[HorizonLine alloc]initWithFrame:CGRectMake(0, self.bounds.size.height-0.5, self.bounds.size.width, 1)];
        [self.contentView addSubview:devider];
        
        [devider setLineColor:RGB(201, 199, 206)];
        devider.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
        
//        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    return self;
}

//子控件
- (void)addSubviews{
    UIImageView *iconView = [[UIImageView alloc] init];
    self.iconView = iconView;
    iconView.image = [UIImage imageNamed:@"default_photo"];
    [self.contentView addSubview:iconView];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:15];
    self.nameLabel = nameLabel;
    nameLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:nameLabel];
    
    UILabel *detailLabel = [[UILabel alloc] init];
     detailLabel.backgroundColor = [UIColor clearColor];
    self.detailLabel = detailLabel;
    detailLabel.textColor = HexRGB(0xb2aeaf);
    detailLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:detailLabel];
    
}

//autolayout约束
- (void)addConstraints{
    
    UIView *subview = self.contentView;
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(subview.mas_top).with.offset(OFFSET);
        make.left.equalTo(subview.mas_left).offset(OFFSET);
        make.bottom.equalTo(subview.mas_bottom).offset(-OFFSET);
        make.width.equalTo(self.iconView.mas_height);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.iconView.mas_top);
        make.left.equalTo(self.iconView.mas_right).offset(OFFSET);
        make.bottom.equalTo(subview.mas_centerY).offset(-5);
        make.right.equalTo(subview.mas_right).offset(-OFFSET);
        make.height.equalTo(@(2 * OFFSET));
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(subview.mas_centerY).offset(5);
        make.left.equalTo(self.nameLabel.mas_left);
        make.right.equalTo(self.nameLabel.mas_right);
        make.height.equalTo(@15);
    }];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
   
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    
    if (highlighted)
    {
        self.backgroundColor = RGB(240, 240, 240);
        
    }else
    {
        self.backgroundColor = [UIColor whiteColor];
        
    }
}

- (void)setCellWithTitle:(NSString *)title iconURL:(NSString *)iconURL image:(UIImage *)image andDetail:(NSString *)detail{
    
    self.nameLabel.text = title;
    self.detailLabel.text = detail;

    if (iconURL != nil) {
         [self.iconView sd_setImageWithURL:[NSURL URLWithString:iconURL] placeholderImage:[UIImage imageNamed:@"default_photo"]];
    }else{
        [self.iconView setImage: image ? image : [UIImage imageNamed:@"default_photo"]];
    }
    
}


@end
